<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            display: flex;
            background-color:cornsilk;
            justify-content: center;
            align-items: center;
            width: 200px;
            height: 100px;
        }
    </style>
</head>

<!-- 
    链接：https://cn.vuejs.org/guide/reusability/custom-directives.html#directive-hooks
 -->
<body>
    <div id="app"></div>
</body>
<script src="js/vue.js"></script>
<script>

    // 将需要的参数传递到指令中，指令对传入的参数做处理
    const FocusDirective = {
        fixed: {
            mounted(el, binding, vnode, prevVnode) { 
              console.log(el, binding);

              el.style.position = 'fixed';
              el.style.left = binding.value.left + 'px';
              el.style.top = binding.value.top + 'px';
            },
        }
    };


    const app = Vue.createApp({
        data() {
            return {
                msg: '指令--接受外部传入参数',
               postData:{
                top: 200,
                left: 200
               }
            }
        },
        // 用于挂载局部指令
        directives: FocusDirective,
        template: `
        <!-- 这里增加 :pos 在指令 console.log(binding) 时，会出现 arg 的值为 pos， 其余的和下面的没有区别  -->
          <div class="box" v-fixed:pos="postData">{{msg}}</div>
          <br>
          <div class="box" v-fixed="postData">{{msg}}</div>

        `,
    }).mount("#app");

</script>

</html>